import 'package:aio/aio.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:app/generated/app_strings.dart';
import 'package:app/src/config/app_data_config.dart';
import 'package:app/src/http/bean/wroks/order_item_info.dart';
import 'package:app/src/http/bean/wroks/product_bean.dart';
import 'package:app/src/http/bean/wroks/special_clauses.dart';
import 'package:app/src/http/model/material_requisition_model.dart';
import 'package:app/src/ui/dialog/dailog.dart';
import 'package:app/src/ui/route/helper/route_helper.dart';
import 'package:app/src/ui/route/works/public/items/ui_base.dart';
import 'package:app/src/ui/route/works/public/items/ui_card.dart';
import 'package:app/src/utils/a_combined_utils.dart';
import 'package:app/src/utils/image_path_utils.dart';

import '../../../../../http/bean/works_2023/commission_bean.dart';
import '../../../../app_ui.dart';

///提成审核详情
class UiPercentageCheckForm {
  static Widget build(BuildContext context, PercentageCheckBean? data,
      {String? title}) {
    if (data?.PercentageCheckInfo == null ||
        data!.PercentageCheckInfo!.length <= 0) {
      return LibUi.getEmptyView();
    }
    return StatefulBuilder(builder: (context, setState) {
      List<Widget> children = [];
      if (StringUtils.isEmpty(title)) {
        title = AppStrings.of(context).DetailedInformation; //详情详情
      }
      children.add(UiBase.title(context, title,
          bottom: AppEdgeInsets.margin_bottom)); //标题
      children.add(_table(context, data!));
      var column = UiBase.column(context, children);
      var card = UiCard.build(context, column);
      return card;
    });
  }

  ///fixme Table表格里的行TabRow;每一行的表格数量必须相等。不然报错。
  ///fixme table嵌套table实现单元格合并。

  //表格线条颜色
  static Color _BorderColor = Colors.black12;

  //表格线条宽度
  static double _width = 1;

  ///背景色表格标题
  static Color _backgroundColorWithTableTitle = Colors.black12;
  static Color _backgroundColorWithWhite = Colors.white;
  static Color _backgroundColorWithGrey = ColorUtils.parseColor("#F7F6F3");
  static StateSetter? _setState = null;

  static Widget _table(BuildContext context, PercentageCheckBean data) {
    if (data?.PercentageCheckInfo == null ||
        data!.PercentageCheckInfo!.length <= 0) {
      return LibUi.getEmptyView();
    }
    //fixme 表格
    return StatefulBuilder(builder: (context, setState) {
      _setState = setState;
      //可以水平滑动的表格
      var scrolltable = _scrolltable(context, data);
      //水平滑动效果
      var singleChildScrollView = SingleChildScrollView(
          child: scrolltable, scrollDirection: Axis.horizontal);
      return Center(
        child: Container(
          child: singleChildScrollView,
          //width: 2400,
        ),
      );
    });
  }

  ///水平滑动表格
  static Widget _scrolltable(BuildContext context, PercentageCheckBean data) {
    if (data.PercentageCheckInfo == null ||
        data.PercentageCheckInfo!.length <= 0) {
      return AppUi.getEmptyView();
    }
    List<PercentageCheckInfoBean> datas = data.PercentageCheckInfo!;
    var firstData = datas[0];
    var isN6016_1 = data.isN6016_1(); //fixme 是否为已退回（已退回可以修改：提成比例，和 逾期扣除）
    var children = <TableRow>[];
    //第一行标题栏
    List<Widget> titleChildren = [];
    titleChildren.add(_text(" " + AppStrings.of(context).TheSerial + " ")); //序号
    titleChildren
        .add(_text(" " + AppStrings.of(context).customername_ + " ")); //客户名称
    titleChildren
        .add(_text(" " + AppStrings.of(context).contractname_ + " ")); //项目名称
    titleChildren
        .add(_text(" " + AppStrings.of(context).cutamount_ + " ")); //收款金额
    titleChildren
        .add(_text(" " + AppStrings.of(context).payrate_ + " ")); //fixme 提成比例
    titleChildren.add(_text(" " + AppStrings.of(context).amount_ + " ")); //提成金额
    titleChildren
        .add(_text(" " + AppStrings.of(context).cutrate_ + " ")); //fixme 逾期扣除
    titleChildren.add(_text(" " +
        AppStrings.of(context).commissionTotal_("100") +
        " ")); //提成合计.这个100百分比erp前端是写死，我这里也写死。
    titleChildren.add(
        _text(" " + AppStrings.of(context).thisAmount_("70") + " ")); //本次提成
    titleChildren.add(_text(
        " " + AppStrings.of(context).commissionAmount_("30") + " ")); //提成结存
    var tableRow = TableRow(
        decoration: BoxDecoration(color: _backgroundColorWithTableTitle),
        children: titleChildren);
    children.add(tableRow);
    //第二行，内容行。
    for (int i = 0; i < datas.length; i++) {
      var data = datas[i];
      List<Widget> rowChildren = [];
      //序号
      var TheSerial = _text("${i + 1}");
      rowChildren.add(TheSerial);
      //客户名称
      var customername = _text("  ${data.customername}  ");
      rowChildren.add(customername);
      //项目名称
      var contractname = _text("  ${data.contractname}  ");
      rowChildren.add(contractname);
      //收款金额
      var cutamount = _text("${data.cutamount}");
      rowChildren.add(cutamount);
      //fixme 提成比例
      if (isN6016_1) {
        var payrate = UiBase.itemWithSigleEdit(context, data?.payrate,
            isAllowEmpty: true,
            isDecimal: true,
            isNegative: false,
            unit: "%",
            isLine: true, onChanged: (value) {
          if (StringUtils.isEmpty(value)) {
            data?.payrate = "0"; //不能为空，为空，提交接口会报数据格式错误。
          } else {
            data?.payrate = value;
          }
        });
        rowChildren.add(payrate);
      } else {
        var payrate = _text("${data.payrate}%");
        rowChildren.add(payrate);
      }

      //提成金额
      var amount = _text("${data.amount}");
      rowChildren.add(amount);
      //fixme 逾期扣除
      if (isN6016_1) {
        var cutrate = UiBase.itemWithSigleEdit(context, data?.cutrate,
            isAllowEmpty: true,
            isDecimal: true,
            isNegative: false,
            unit: "%",
            isLine: true, onChanged: (value) {
          if (StringUtils.isEmpty(value)) {
            data?.cutrate = "0"; //不能为空，为空，提交接口会报数据格式错误。
          } else {
            data?.cutrate = value;
          }
        });
        rowChildren.add(cutrate);
      } else {
        var cutrate = _text("${data.cutrate}%");
        rowChildren.add(cutrate);
      }
      //提成合计
      var commissionTotal = _text("${data.commissionTotal}");
      rowChildren.add(commissionTotal);
      //本次提成
      var thisAmount = _text("${data.thisAmount}");
      rowChildren.add(thisAmount);
      //提成结存
      var commissionAmount = _text("${data.commissionAmount}");
      rowChildren.add(commissionAmount);
      var tableRow = TableRow(
          // decoration: BoxDecoration(color: _getColor(_index + 1)),
          decoration: BoxDecoration(color: _getColor(i)),
          children: rowChildren);
      children.add(tableRow);
    }
    var table = Table(
      border: TableBorder.all(
          color: _BorderColor, width: _width, style: BorderStyle.solid),
      defaultVerticalAlignment: TableCellVerticalAlignment.middle,
      columnWidths: {
        0: IntrinsicColumnWidth(),
        1: IntrinsicColumnWidth(),
        2: IntrinsicColumnWidth(),
        3: IntrinsicColumnWidth(),
        4: IntrinsicColumnWidth(),
        5: IntrinsicColumnWidth(),
        6: IntrinsicColumnWidth(),
        7: IntrinsicColumnWidth(),
        8: IntrinsicColumnWidth(),
        9: IntrinsicColumnWidth(),
        10: IntrinsicColumnWidth(),
        11: IntrinsicColumnWidth(),
        12: IntrinsicColumnWidth(),
        13: IntrinsicColumnWidth(),
        14: IntrinsicColumnWidth(),
        15: IntrinsicColumnWidth(),
        16: IntrinsicColumnWidth(),
        17: IntrinsicColumnWidth(),
        18: IntrinsicColumnWidth(),
        19: IntrinsicColumnWidth(),
      },
      children: children,
    );
    return table;
  }

  //fixme 条目颜色背景
  static Color _getColor(int i) {
    Color _color = _backgroundColorWithWhite;
    //print("=============$i ${i%2}");
    if (i % 2 == 0) {
      _color = _backgroundColorWithGrey;
    }
    return _color;
  }

  //文本
  static Widget _text(String? text, {bool isStart = false}) {
    // return Center(
    //   child: Text(text ?? ""),
    // );
    Widget child;
    if (isStart) {
      var textKey = Text(
        text ?? "",
        style: AppStyle.textStyleWithCard(),
      );
      var value = "*";
      var startText = Text(
        value,
        softWrap: true,
        style: AppStyle.textStyleWithCardRed(),
      );
      child = SizedBox(
        child: Row(
          children: [textKey, startText],
          mainAxisAlignment: MainAxisAlignment.center,
        ),
        width: 120,
      );
    } else {
      child = Text(text ?? "");
    }
    return Center(
      child: ConstrainedBox(
        constraints: BoxConstraints(
          minHeight: 36.0, //最小高度
        ),
        child: Center(
          child: child,
        ),
      ),
    );
  }
}
